package com.smart.translator.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.smart.translator.data.AppConfig;
import org.json.JSONObject;

/**
 * 配置管理工具类
 */
public class ConfigManager {
    private static final String TAG = "ConfigManager";
    private static final String PREF_NAME = "app_config_preferences";
    private static final String KEY_TTS_PROVIDER = "tts_provider";
    private static final String KEY_ASR_PROVIDER = "asr_provider";
    private static final String KEY_SERVER_URL = "server_url";
    private static final String KEY_APP_VERSION = "app_version";
    private static final String KEY_CM3_VERSION = "cm3_version";
    private static final String KEY_FLASH_VERSION = "flash_version";
    
    private static ConfigManager instance;
    private SharedPreferences preferences;
    private AppConfig currentConfig;

    private ConfigManager(Context context) {
        preferences = context.getApplicationContext()
                .getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        loadConfigFromPreferences();
    }

    public static synchronized ConfigManager getInstance(Context context) {
        if (instance == null) {
            instance = new ConfigManager(context);
        }
        return instance;
    }

    /**
     * 保存配置信息
     */
    public void saveConfig(AppConfig config) {
        this.currentConfig = config;
        
        try {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(KEY_TTS_PROVIDER, config.getTtsProvider());
            editor.putString(KEY_ASR_PROVIDER, config.getAsrProvider());
            editor.putString(KEY_SERVER_URL, config.getServerUrl());
            editor.putString(KEY_APP_VERSION, config.getAppVersion());
            editor.putString(KEY_CM3_VERSION, config.getCm3Version());
            editor.putString(KEY_FLASH_VERSION, config.getFlashVersion());
            editor.apply();
            
            Log.d(TAG, "配置信息已保存: " + config.toString());
        } catch (Exception e) {
            Log.e(TAG, "保存配置信息失败", e);
        }
    }

    /**
     * 从本地存储加载配置信息
     */
    private void loadConfigFromPreferences() {
        try {
            currentConfig = new AppConfig();
            currentConfig.setTtsProvider(preferences.getString(KEY_TTS_PROVIDER, ""));
            currentConfig.setAsrProvider(preferences.getString(KEY_ASR_PROVIDER, ""));
            currentConfig.setServerUrl(preferences.getString(KEY_SERVER_URL, ""));
            currentConfig.setAppVersion(preferences.getString(KEY_APP_VERSION, ""));
            currentConfig.setCm3Version(preferences.getString(KEY_CM3_VERSION, ""));
            currentConfig.setFlashVersion(preferences.getString(KEY_FLASH_VERSION, ""));
            
            Log.d(TAG, "配置信息已加载: " + currentConfig.toString());
        } catch (Exception e) {
            Log.e(TAG, "加载配置信息失败", e);
            currentConfig = new AppConfig();
        }
    }

    /**
     * 获取当前配置
     */
    public AppConfig getCurrentConfig() {
        return currentConfig;
    }

    /**
     * 获取TTS提供商
     */
    public String getTtsProvider() {
        return currentConfig != null ? currentConfig.getTtsProvider() : "";
    }

    /**
     * 获取ASR提供商
     */
    public String getAsrProvider() {
        return currentConfig != null ? currentConfig.getAsrProvider() : "";
    }

    /**
     * 获取服务器URL
     */
    public String getServerUrl() {
        return currentConfig != null ? currentConfig.getServerUrl() : "";
    }

    /**
     * 获取应用版本
     */
    public String getAppVersion() {
        return currentConfig != null ? currentConfig.getAppVersion() : "";
    }

    /**
     * 获取CM3版本
     */
    public String getCm3Version() {
        return currentConfig != null ? currentConfig.getCm3Version() : "";
    }

    /**
     * 获取Flash版本
     */
    public String getFlashVersion() {
        return currentConfig != null ? currentConfig.getFlashVersion() : "";
    }

    /**
     * 检查配置是否已加载
     */
    public boolean isConfigLoaded() {
        return currentConfig != null && 
               (currentConfig.getTtsProvider() != null && !currentConfig.getTtsProvider().isEmpty() ||
                currentConfig.getAsrProvider() != null && !currentConfig.getAsrProvider().isEmpty());
    }

    /**
     * 清除配置信息
     */
    public void clearConfig() {
        this.currentConfig = new AppConfig();
        
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.apply();
        
        Log.d(TAG, "配置信息已清除");
    }
} 